package com.find.answer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.find.answer.domain.dto.PageDto;
import com.find.answer.domain.dto.DishDto;
import com.find.answer.domain.http.Result;
import com.find.answer.domain.po.Dish;
import com.find.answer.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Seon
 * @date 2022/10/25 21:47
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @GetMapping("/page")
    public Result<?> getDishList(PageDto answer,
                                 @RequestParam(value = "name",required = false) String name) {
        Page<DishDto> dishDtoPage = dishService.getDishDtoPage(answer, name);

        return Result.success(dishDtoPage);
    }

    @GetMapping("/{id}")
    public Result<?> getDishById(@PathVariable("id") Long id) {
        Dish result = dishService.getWithFlavors(id);
        return Result.success(result);
    }

    @PostMapping("")
    public Result<?> addDish(@RequestBody DishDto dishDto) {
        boolean save = dishService.saveWithFlavors(dishDto);
        if (save) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @PostMapping("/status/{status}")
    public Result<?> updateStatusById(@PathVariable("status") Integer status,
                                      @RequestParam("ids") List<Long> ids) {
        Dish dish = new Dish();
        ids.forEach(id -> {
            dish.setId(id);
            dish.setStatus(status);
            dishService.updateById(dish);
        });
        return Result.success("修改成功");
    }

    @PutMapping("")
    public Result<?> updateDish(@RequestBody DishDto dishDto) {
        dishService.updateWithFlavors(dishDto);
        return Result.success("修改成功");
    }

    @DeleteMapping("")
    public Result<?> deleteDish(@RequestParam("ids") List<Long> ids) {
        dishService.deleteWithFlavors(ids);
        return Result.success("删除成功");
    }

    @GetMapping("/list")
    public Result<?> getDishesWithFlavorById(Dish dish) {
        Long categoryId = dish.getCategoryId();
        Integer status = dish.getStatus();
        
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, categoryId);
        wrapper.eq(Dish::getStatus, status);
        wrapper.orderByDesc(Dish::getUpdateTime);
        List<Dish> dishList = dishService.list(wrapper);

        List<DishDto> collect = dishList.stream().map(items -> {
            Long id = items.getId();
            return dishService.getWithFlavors(id);
        }).collect(Collectors.toList());

        return Result.success(collect);
    }
}
